package com.bwei.like.mynewstop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
import android.view.ViewGroup;
>>>>>>> NewsTop
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.bwei.like.mynewstop.Bean.NewsCategoryRoot;
import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.activity.WebView_activity;
import com.bwei.like.mynewstop.adapater.NewsCategoryAdapater;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
import com.bwei.like.mynewstop.app.MyApplication;
>>>>>>> NewsTop
import com.bwei.like.mynewstop.base.BaseData;
import com.bwei.like.mynewstop.base.BaseFragment;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.bwei.like.mynewstop.utils.LogUtils;
import com.bwei.like.mynewstop.utils.URLUtils;
import com.bwei.like.mynewstop.xListView.XListView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.utils.L;


import org.xutils.common.util.LogUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

/**
 * Created by LiKe on 2016/11/1.
 */
public class MainFragment extends BaseFragment implements XListView.IXListViewListener {

    //刷新数据集合
    ArrayList<NewsCategoryRoot.DataBean> refrshList = new ArrayList<>();
    private XListView xListView;
    private ArrayList<NewsCategoryRoot.DataBean> data;
    private NewsCategoryAdapater adapater;
    private BaseData baseData;
    private String URLPATH;

    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.mian_fragment_item, null);
        xListView = (XListView) view.findViewById(R.id.xListView);
        return view;
    }

    @Override
    protected void initActivity() {
        String title = this.getArguments().getString("title");
        URLPATH = URLUtils.URL_CATEGROY_PATH + title;
        xListView.setPullLoadEnable(true);
        xListView.setXListViewListener(this);
        //请求网络获取数据
        initData("", BaseData.LONGTTIME, true);
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
        LogUtils.i("TAG++++++++", "initActivity");
>>>>>>> NewsTop


        //设置点击条目监听
        xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                infoReadItem(position);

                //点击条目跳转界面
                Intent intent = new Intent(getActivity(), WebView_activity.class);
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
                intent.putExtra("DataBean",refrshList.get(position));
=======
                intent.putExtra("DataBean", refrshList.get(position));
>>>>>>> NewsTop
                startActivity(intent);
            }
        });
    }


    private void infoReadItem(int position) {
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
       CommonUtils.saveBoolean(refrshList.get(position-1).getTitle(),true);
        if (adapater!=null)
        adapater.notifyDataSetChanged();
=======
        CommonUtils.saveBoolean(refrshList.get(position - 1).getTitle(), true);
        if (adapater != null)
            adapater.notifyDataSetChanged();
>>>>>>> NewsTop
    }

    public static Fragment getFragment(String title) {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }


    private void initData(final String requestPath, final long refreshTime, final boolean flag) {

        baseData = new BaseData() {
            @Override
            public void setResultData(String path) {
                onLoad();
                if (!TextUtils.isEmpty(path)) {
                    Gson gson = new Gson();
                    NewsCategoryRoot newsCategoryRoot = gson.fromJson(path, NewsCategoryRoot.class);
                    data = (ArrayList<NewsCategoryRoot.DataBean>) newsCategoryRoot.getData();
                    if (flag) {
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935

//                        for (int i = 0; i < refrshList.size(); i++) {
//                            if (refrshList.get(i).getLabel().equals("置顶")){
//                                refrshList.remove(refrshList.get(i).getLabel().equals("置顶"));
//                            }
//                        }
//
//                        for (int i = 0; i < data.size(); i++) {
//                            if (!refrshList.contains(data.get(i))){
//
//                            }
//                        }
                        refrshList.addAll(0,data);
                    } else {
//                        for (int i = 0; i < data.size(); i++) {
//                            if (!refrshList.contains(data.get(i))) {
//                            }
//                        }
                                refrshList.addAll(data);
                    }
                }

                for (int i = 0; i < refrshList.size(); i++) {
                    for (int j = refrshList.size()-1;j>i;j--){
                        if (refrshList.get(i).getTitle().equals(refrshList.get(j).getTitle())){
=======
                        refrshList.addAll(0, data);
                    } else {
                        refrshList.addAll(data);
                    }
                }
                for (int i = 0; i < refrshList.size(); i++) {
                    for (int j = refrshList.size() - 1; j > i; j--) {
                        if (refrshList.get(i).getTitle().equals(refrshList.get(j).getTitle())) {
>>>>>>> NewsTop
                            refrshList.remove(j);
                        }
                    }
                }


                if (adapater == null) {
                    adapater = new NewsCategoryAdapater(refrshList, getActivity());
                    xListView.setAdapter(adapater);
                } else {
                    adapater.notifyDataSetChanged();
                }
            }


            @Override
            protected void setFailResult(int error_Net) {
            }
        };
        baseData.getData(URLPATH + requestPath, refreshTime);
    }


    @Override
    public void onRefresh() {
        initData("&bd_loc_time=" + (System.currentTimeMillis() / 1000), BaseData.NOTIME, true);
    }

    @Override
    public void onLoadMore() {
        initData(getLoadTimeStamp(), BaseData.NOTIME, false);
    }

    private void onLoad() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime(getTimeString());
    }

    /**
     * 获取当前的时间戳
     *
     * @return
     */
    public String getLoadTimeStamp() {
        return "&max_behot_time=" + System.currentTimeMillis() / 1000;
    }

    @Override
    public void onStart() {
        super.onStart();
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
        //   adapater = null;
=======
        LogUtils.i("TAG++++++++", "onStart");
>>>>>>> NewsTop
    }

    @Override
    public void onStop() {
        super.onStop();
        adapater = null;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
        LogUtils.i("TAG++++++++", "onStop");
>>>>>>> NewsTop
    }

    private static String getTimeString() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        return formatter.format(new Date(System.currentTimeMillis()));
    }
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======


>>>>>>> NewsTop
}
